package com.hhs.storage.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;

/**
 * @Auther: shadow
 * @Date: 2018/11/15 17:51
 * @Description:
 */
@Slf4j
public class FileUpload {
    public static String writeUploadAllFile(MultipartFile file,String folderName, String module) {
        if(folderName == null || "".equals(folderName)) return null;
        String fileName = "";
        try {
            if (!file.isEmpty()) {
                File fileSourcePath = new File(folderName,module);
                if (!fileSourcePath.exists()) {
                    fileSourcePath.mkdirs();
                }
                fileName = file.getOriginalFilename();
                String extname = FilenameUtils.getExtension(fileName);
                fileName = Math.abs(file.getOriginalFilename().hashCode()) + RandomUtils.createRandomString( 4 ) + "." + extname;
                File outFile = new File(fileSourcePath ,fileName);
                file.transferTo(outFile);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return File.separator+module+File.separator + fileName;
    }
    /**
     *
     * @param file      上传的文件
     * @param folderName  统一的文件夹地址
     * @param module    该图片的模块文件夹名
     *                  banner:banner
     *                  build ：在建工地
     *                  decoration: 一键装修
     *                  editor :富文本编辑器
     *                  goodbad:优劣对比
     *                  material: 主材包
     *                  activity: 活动
     * @return
     */
    public static String writeUploadFile(MultipartFile file,String folderName, String module) {
        if(folderName == null || "".equals(folderName)) return null;
        String fileName = "";
        try {
            if (!file.isEmpty()) {
                File fileSourcePath = new File(folderName,module);
                if (!fileSourcePath.exists()) {
                    fileSourcePath.mkdirs();
                }
                fileName = file.getOriginalFilename();
                String extname = FilenameUtils.getExtension(fileName);
                String allowImgFormat = "gif,jpg,jpeg,png,mp3,mp4,mp2,avi,flv,mpeg";
                if (!allowImgFormat.contains(extname.toLowerCase())) {
                    return null;
                }
                fileName = Math.abs(file.getOriginalFilename().hashCode()) + RandomUtils.createRandomString( 4 ) + "." + extname;
                File outFile = new File(fileSourcePath ,fileName);
                file.transferTo(outFile);
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        return File.separator+module+File.separator + fileName;
    }

    public static void  deleteFile(String folderName, String fileName){
        if(fileName.indexOf("\\upload")>-1){
            fileName=fileName.replace("\\upload","");
        }else if(fileName.indexOf("/upload")>-1){
            fileName=fileName.replace("/upload","");
        }

        File target = new File(folderName,fileName);
        target.delete();
    }
}
